
/*
 * WindowMalfunction class
 * This class represents when a window malfunctions. Extends from Event
 * 
 * James Zhang
 * ID: 3069886
 * July 17, 2012
 */

public class WindowMalfunction extends Event
{
    /**
     * Default constructor
     *
     * @param g - Passed in the governing GreenhouseControls object reference
     * @param delayTime - Delay time for when to start this event
     */
    public WindowMalfunction( GreenhouseControls g, long delayTime )
    {
        //Calls super
        super( g, delayTime );
    }

    /**
     * Overridden run method which sets the window to false and throws a
     * ControllerException
     */
    public synchronized @Override
    void run()
    {
        while ( timePassed < delay )
        {
            try
            {
                thread.sleep( 1000 );
                timePassed += 1000;
            } catch ( InterruptedException e )
            {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
        gc.crashTime = this.delay;
        gc.gui.jTextArea1.append( toString() + "\n" );
        gc.setVariable( "windowok", false );
        try
        {
            errortime();
        } catch ( ControllerException ex )
        {
        }
        this.setThread( true );
        gc.shutdown( 1 );
    }

    //Method which throws the exception
    public void errortime() throws ControllerException
    {
        throw new ControllerException( "Window Malfunction!", 1 );
    }

    /**
     * Override of toString()
     *
     * @return - Returns event in sentence form
     */
    public String toString()
    {
        return "Problem occured!";
    }
}